package com.me.service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.me.json.China;
import com.me.json.Province;
import com.me.mapper.CarMapper;
import com.me.mapper.MyOrderMapper;
import com.me.mapper.OrderMessageMapper;
import com.me.mapper.ReceiveTimeMapper;
import com.me.mapper.ShippingAddressMapper;
import com.me.mapper.UserMapper;
import com.me.pojo.Car;
import com.me.pojo.Enhance;
import com.me.pojo.MyOrder;
import com.me.pojo.OrderMessage;
import com.me.pojo.ReceiveTime;
import com.me.pojo.ShippingAddress;
import com.me.pojo.User;

public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private CarMapper carMapper;
	
	@Autowired
	private ShippingAddressMapper shippingAddressMapper;
	
	@Autowired
	private MyOrderMapper myOrderMapper;
	
	@Autowired
	private OrderMessageMapper orderMessageMapper;
	
	@Autowired
	private ReceiveTimeMapper receiveTimeMapper;
	
	public UserMapper getUserMapper() {
		return userMapper;
	}
	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}
	
	public void signUp(User user) 
	{
		userMapper.signup(user);
	}
	public String selectByUsername(String username) 
	{
		int i=userMapper.selectByUsername(username);
		System.out.println(i);
		if(i!=0)
		{
			return "false";
		}
		return "true";
	}
	public String login(String username, String password) {
		User login=new User();
		login.setUsername(username);
		login.setPassword(password);
		int a=userMapper.login(login);
		if(a==0)
		{
			return "consumeLogin";
		}
		
		return "consumeBase";	
	}
	public User selectUser(String username, String password) {
		User login=new User();
		login.setUsername(username);
		login.setPassword(password);
		return userMapper.selectByUP(login);
	}
	public void updatePic(User user) 
	{
		userMapper.updatePic(user);
	}
	public int insertCar(Car record) {
		
		return carMapper.insertSelective(record);
	}
	
	public List<Enhance> selectEnhanceByUserId(Integer userId) {
		return carMapper.selectEnhanceByUserId(userId);
	}
	
	public int deleteCarByGoodsid(Integer id) {
		return carMapper.deleteByGoodsid(id);
		
	}

	public String parseJSON() {		
		try {
			 InputStream is=new FileInputStream("E:/code3/MyMall/src/main/resources/allprovinces.json");
			 InputStreamReader isr = new InputStreamReader(is,"UTF-8");
			 BufferedReader br=new BufferedReader(isr);
			 String address = "";  
			 String line = null;  
			 while((line = br.readLine()) != null) {  
				 address += line;  
			 } 
			 br.close();
			 return address;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public Province selectCityByProvinceName(String province) {
		
		String json = parseJSON();	
		China china = new China();
		Gson gson = new Gson();
		china = gson.fromJson(json, China.class);
		List<Province> list =china.getProvincesList();
		for(Province a:list)
		{
			if(province.equals(a.getName()))
			{
				return a;
			}
		}
		return null;
	}
	
	public int insertAddressSelective(ShippingAddress record) {
		return shippingAddressMapper.insertSelective(record);
	}
	
	public List<ShippingAddress> selectAddressByUserid(Integer userid) {
		return shippingAddressMapper.selectAddressByUserid(userid);
	}
	
	public int deleteAddressByPrimaryKey(Integer id) {
		return shippingAddressMapper.deleteByPrimaryKey(id);
	}
	
	public void insertOrderSelective(Integer userid,Integer addressId) {
		MyOrder order=new MyOrder();
		order.setUserId(userid);
		order.setCode(System.currentTimeMillis()+"");
		order.setShippingAddressId(addressId);
		order.setOrderStatusId(1);
		myOrderMapper.insertSelective(order);
		List<Enhance> list=selectEnhanceByUserId(userid);
		OrderMessage orderMessage=new OrderMessage();
		for(Enhance enhance:list)
		{
			orderMessage.setGoodsId(enhance.getGoods().getId());
			orderMessage.setGoodsnum(enhance.getCar().getGoodsnum());
			orderMessage.setOrderId(order.getId());
			orderMessageMapper.insertSelective(orderMessage);
		}
		deleteCarByUSerid(userid);
	}
	
	public int deleteCarByUSerid(Integer userid) {
		return carMapper.deleteByUSerid(userid);
	}
	
	public List<Enhance> selectOrderByUserId(Integer userId) {
		return myOrderMapper.selectOrderByUserId(userId);	
	}
	
	public int updateMyOrderByPrimaryKeySelective(MyOrder record) {	
		return myOrderMapper.updateByPrimaryKeySelective(record);
	}
	public int insertDateSelective(ReceiveTime record) {
		
		return receiveTimeMapper.insertSelective(record);
	}
	public ReceiveTime selectDateByOrderId(Integer orderid) {
		return receiveTimeMapper.selectDateByOrderId(orderid);
	}
	public User selectUserById(Integer userid) {
		return userMapper.selectUserById(userid);
	}

	

}
